<?php
/*******************************************/
/*              move conn  To  conn2           */
/*******************************************/
set_time_limit(0);
$bet_dbhost = "192.168.5.128";
$bet_dbuser = "kacino2";
$bet_dbpass = "cyriac_0426";
$bet_dbname = "bet7starLonUsbet66Us";
if (! $conn = @mysql_connect($bet_dbhost, $bet_dbuser, $bet_dbpass)) {
	print "連線錯誤：無法連線至資料庫主機！1";
	exit();
}
//mysql_query("SET NAMES 'utf8'", $conn);
mysql_query("SET NAMES 'latin1'", $conn);
if (! @mysql_select_db($bet_dbname, $conn)) {
	print "連線錯誤：無法連線至資料庫主機！2";
	exit();
}

$bet_dbhost = "192.168.5.128";
$bet_dbuser = "kacino";
$bet_dbpass = "cyriac_0426";
$bet_dbname = "betLonUBestusUTF8";
if (! $conn2 = @mysql_connect($bet_dbhost, $bet_dbuser, $bet_dbpass)) {
	print "連線錯誤：無法連線至資料庫主機！11";
	exit();
}
mysql_query("SET NAMES 'utf8'", $conn2);
if (! @mysql_select_db($bet_dbname, $conn2)) {
	print "連線錯誤：無法連線至資料庫主機！22";
	exit();
}

global $arrExistID;
$arrExistID = array();

function ChangeMemberCommissionToOtherSite($oldID, $newID){
	global $conn, $conn2;

	$strSQL = "select no, wth as Comm1, wtt as Comm2, wtu as Comm3, wht as Comm4, whu as Comm5, wttu as Comm6, w2 as Comm12, w3 as Comm13, wsd as Comm14, wbs as Comm15, whsd as Comm16, whbs as Comm17, w3f as Comm7, w3f as Comm8, w3f as Comm9, w3f as Comm10, w4f as Comm11 from init where MID = ".$oldID;

	if(!$RS = mysql_query($strSQL, $conn)){
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
		exit();
	}

	$strTmp = "('".$newID."', '4','%s', '%s', '%s')";
	$arrValues = array();
	while($arrRows = mysql_fetch_assoc($RS)){
		for($intBT = 1; $intBT <= 22; $intBT++){
			$arrValues[] = sprintf($strTmp, $intBT, $arrRows['no'], (100 - $arrRows['Comm'.$intBT]));
		}
	}

	if(sizeof($arrValues) == 0)exit("No Commission Data");
	$strSQL = "insert into bet_Commission(`MemID`, `LottoID`, `Bet_Type`, `Gamble_Type`, `Commission`) values".implode(",", $arrValues);

	if(!mysql_query($strSQL,$conn2))if(mysql_errno($conn2) != 1062)exit(mysql_error($conn2)."<br>\n");
	return true;
}


function ChangeMemberLimitToOtherSite($oldID, $newID){
	global $conn, $conn2;
	$strSQL = "select oth as BLt1, ott as BLt2, otu as BLt3, oht as BLt4, ohu as BLt5, ottu as BLt6, o2 as BLt12, o3 as BLt13, osd as BLt14, obs as BLt15, ohsd as BLt16, ohbs as BLt17, o3f as BLt7, o3f as BLt8, o3f as BLt9, o3f as BLt10, o4f as BLt11, 
	lth as ELt1, ltt as ELt2, ltu as ELt3, lht as ELt4, lhu as ELt5, lttu as ELt6, l2 as ELt12, l3 as ELt13, lsd as ELt14, lbs as ELt15, lhsd as ELt16, lhbs as ELt17, l3f as ELt7, l3f as ELt8, l3f as ELt9, l3f as ELt10, l4f as ELt11 from players where MID = ".$oldID;

	if(!$RS = mysql_query($strSQL, $conn)){
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
		exit();
	}

	$strTmp = "('".$newID."', '4','%s', '%s', '%s')";
	$arrValues = array();
	while($arrRows = mysql_fetch_assoc($RS)){
		for($intBT = 1; $intBT <= 22; $intBT++){
			$arrValues[] = sprintf($strTmp, $intBT, 0, $arrRows['ELt'.$intBT]);
			$arrValues[] = sprintf($strTmp, $intBT, 1, $arrRows['BLt'.$intBT]);
		}
	}

	if(sizeof($arrValues) == 0)exit("No Limit Data");
	$strSQL = "insert into bet_MemberLimit(`MemID`, `LottoID`, `Bet_Type`, `LimitType`, `LimitValue`) values".implode(",", $arrValues);

	if(!mysql_query($strSQL,$conn2))if(mysql_errno($conn2) != 1062)exit(mysql_error($conn2)."<br>\n");
	return true;
}


function ChangeMemberToOtherSite(){
	global $conn, $conn2, $arrID, $arrExistID;

	$strSQL = "select a.MID, a.name, a.ACC, a.pwd, a.pct, a.pct2, a.ttl as CreditLine, a.isSup, a.isView, b.lv, b.MID2, b.MID3, b.MID4, b.MID5, b.UPMID from players a, stratum b where a.MID = b.MID and a.stEnb = 1 and a.isStop = 0 and b.lv in (1, 2, 3, 4, 5) order by b.lv";

	if(!$RS = mysql_query($strSQL, $conn)){
		print $strSQL."<br>\n";
		print mysql_error($conn)."<br>\n";
		exit();
	}

	$arrLVToGroup = array(5 => 1, 4 => 2, 3 => 3, 2 => 4, 1 => 5);
	$strTmp = "insert into bet_Member(`ID`, `Account`, `Password`, `Name`, `MemberGroup`, `GambleType`, `isAllAccount`, `isSupple`, `CreditLine`, `Scale`, `MainScale`, `ParentID`, `SubID`, `MainID`, `StockID`, `DirectorID`, `AddDate`) values('%d', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '2009-05-31')";
	$arrValues = array();
	$arrComp = array();
	$arrDirector = array();
	$arrStock = array();
	while($Rows = mysql_fetch_object($RS)){
		$intGLine = 0;

		//取的會員分盤種類
		if($Rows -> lv == 5){
			$strSQL = "select MID, no from init where MID  = ".$Rows -> MID;
			if(!$RS1 = mysql_query($strSQL, $conn)){
				print $strSQL."<br>\n";
				print mysql_error($conn)."<br>\n";
				exit();
			}

			$Rows1 = mysql_fetch_object($RS1);
			$intGLine = $Rows1 -> no;
		}

		$intPct1 = 0;
		$intPct2 = 0;
		switch($Rows -> lv){
			case 1:
				$intPct1 = $Rows -> pct;
				$arrComp[$Rows -> MID] = $Rows -> pct;
				break;

			case 2:
				$intPct1 = $arrComp[$Rows -> MID2] - $Rows -> pct;
				$arrDirector[$Rows -> MID] = $Rows -> pct;
				break;

			case 3:
				$intPct1 = $arrDirector[$Rows -> MID3] - $Rows -> pct;
				break;

			case 4:
				$intPct1 = $Rows -> pct;
				$intPct2 = $Rows -> pct2;
				break;
		}

		$strName = iconv("GB2312", "UTF-8", $Rows -> name);
		$strSQL = sprintf($strTmp, $Rows -> MID, $Rows -> ACC, $Rows -> pwd, addslashes($strName), $arrLVToGroup[$Rows -> lv], $intGLine, $Rows -> isView, $Rows -> isSup, $Rows -> CreditLine, ($intPct1 / 100), ($intPct2 / 100), $Rows -> UPMID, $Rows -> MID5, $Rows -> MID4, $Rows -> MID3, $Rows -> MID2);

		if(!mysql_query($strSQL, $conn2)){
			if(mysql_errno($conn2) == 1062){
				print $Rows -> ACC."@@@@<br>\n";
			}else{
				print $strSQL."<br>\n";
				print mysql_error($conn2)."<br>\n";
				exit();
			}
		}

		//$newMemIDS = mysql_insert_id($conn2);
		print $Rows -> ACC."==".$Rows -> MID."<br>\n";
		ChangeMemberCommissionToOtherSite($Rows -> MID, $Rows -> MID);
		ChangeMemberLimitToOtherSite($Rows -> MID, $Rows -> MID);
		flush();
	}

	
}
ChangeMemberToOtherSite();
?>OK;